package com.apartmentManage.controller;

import com.apartmentManage.entity.Apartment;
import com.apartmentManage.entity.ApartmentTutor;
import com.apartmentManage.service.ApartmentService;
import com.apartmentManage.service.ApartmentTutorService;
import com.apartmentManage.until.Renter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/apartmentTutor")
public class ApartmentTutorController {

    @Autowired
    private ApartmentTutorService apartmentTutorService;

    @Autowired
    private ApartmentService apartmentService;

    @GetMapping("/getAll")
    public String getAll(Model model){
        List<ApartmentTutor>  result=apartmentTutorService.getAll();
        Renter renter = new Renter(result != null ? true : false, result);
        model.addAttribute("renter",renter);
        return "pages/apartmentTutor/list";
    }

    @GetMapping("/getByRequirement")
    public String getByRequirement(ApartmentTutor apartmentTutor,Model model){
        List<ApartmentTutor> result =
                apartmentTutorService.getByRequirement(apartmentTutor);
        Renter renter = new Renter(result != null ? true : false, result);
        model.addAttribute("renter",renter);
        return "pages/apartmentTutor/List";
    }

    @GetMapping("/getByNo/{no}")
    public String getByNo(@PathVariable("no") String no,Model model){
        ApartmentTutor result =apartmentTutorService.getByNo(no);
        List<Integer> allNo = apartmentService.getAllNo();
        model.addAttribute("allNo",allNo);
        model.addAttribute("result",result);
        return "pages/apartmentTutor/update";
    }

    @GetMapping("/addView")
    public String addView(Model model){
        List<Integer> allNo = apartmentService.getAllNo();
        model.addAttribute("allNo",allNo);
        return "pages/apartmentTutor/add";
    }

    @PostMapping("/save")
    public String save(ApartmentTutor apartmentTutor){
        if (apartmentTutor.getApartmentNo()==0)
            return "redirect:/apartmentTutor/addView";
        boolean flag =apartmentTutorService.save(apartmentTutor);
        new Renter(flag,flag ? "添加成功^_^" : "添加失败-_-");
        return "redirect:/apartmentTutor/getAll";
    }

    @PostMapping("/update")
    public String update(ApartmentTutor apartmentTutor){
        boolean flag =apartmentTutorService.update(apartmentTutor);
        new Renter(flag,flag ? "更新成功^_^" : "更新失败-_-");
        return "redirect:/apartmentTutor/getAll";
    }

    @GetMapping("/delete/{no}")
    public String delete(@PathVariable("no")  String no){
        boolean flag =apartmentTutorService.delete(no);
        new Renter(flag,flag ? "删除成功^_^" : "删除失败-_-");
        return "redirect:/apartmentTutor/getAll";


    }
}
